<!--
  - Copyright(c)  2022 厦门外里科技有限公司 All rights reserved.
  -
  - https://www.wailikeji.com
  -
  - 版权所有，侵权必究！
  -->

<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="formData.phoneData" type="text" maxlength="11" placeholder="手机号" :focus="isFocus">
				</wInput>
				<wInput v-if="loginType=='code'" v-model="formData.verCode" type="number" maxlength="4"
					placeholder="验证码" isShowCode ref="runCode" @setCode="getVerCode()"></wInput>
				<wInput v-if="loginType=='pwd'" v-model="formData.password" type="password" maxlength="11"
					placeholder="密码"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>

			<!-- 其他登录 -->
			<!-- 			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
			</view>
 -->
			<!-- 底部信息 -->
			<view class="footer">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'"> 同意</text>
				<navigator url="/pages/my/xieyi" open-type="navigate">《协议》</navigator>
			</view>
			<!-- 底部信息 -->
			<view class="footer">
				<navigator v-if="loginType=='code'" url="" open-type="navigate" @click="changeType">密码登录</navigator>
				<navigator v-if="loginType=='pwd'" url="" open-type="navigate" @click="changeType">验证码登录</navigator>
			</view>
			<u-popup class="qrcode" v-model="show" mode="bottom" border-radius="30" :closeable="true">
				<view class="title" style="font-size:40rpx ;margin: 40rpx; text-align: center">微信扫码登录</view>
				<view style="height: 600rpx; border-radius: 30rpx; margin: 100rpx 100rpx; background-color: #18B566; ">
					<u-image style="height: 100%; width: 100%;" :src="erweima">
						<u-loading slot="loading"></u-loading>
					</u-image>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/login/watch-input.vue' //input
	import wButton from '../../components/login/watch-button.vue' //button
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				formData: {
					phoneData: '', //用户/电话
					verCode: "", //验证码
				},
				loginType: "code",
				showAgree: false, //协议是否选择
				isRotate: false, //是否加载旋转
				isFocus: true, // 是否聚焦
				show: false, //弹出层
				erweima: '../../static/qun.png'
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		onShow() {
			this.$store.dispatch('getUserInfo')
				.then(res => {
					if (res.code == 200) {
						this.$Router.replaceAll('/pages/index/index')
					}
				}).catch(e => {

				});
		},
		methods: {
			changeType() {
				console.log("changeType")
				if (this.loginType == "code") {
					this.loginType = "pwd"
				} else if (this.loginType == "pwd") {
					this.loginType = "code"
				}
			},
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			/**
			 * 获取验证码
			 */
			getVerCode() {
				if (_this.formData.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}

				this.$api.api('loginPhoneCode', {
						"phone": _this.formData.phoneData,
						"deptId": "1067246875800000066"
					})
					.then(res => {
						if (res.code == 200) {

							//启动倒计时
							_this.$refs.runCode.$emit('runCode', 60);
							uni.showToast({
								icon: 'none',
								position: 'bottom',
								title: '验证码已发送'
							});
						} else {
							uni.showToast({
								icon: 'none',
								position: 'bottom',
								title: res.msg
							});
							if (res.code == 10037) {
								_this.$refs.runCode.$emit('runCode', res.data.leftTime);
							}
						}
					})
					.catch((e) => {
						resolve(false);
					});

			},

			/**
			 * 开始登录
			 * @param {Object} e
			 */
			startLogin(e) {
				console.log(e)
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}

				if (this.formData.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不能为空'
					});
					return;
				}
				if (this.loginType == "code" && this.formData.verCode.length != 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '验证码不正确'
					});
					return false;
				}

				if (this.loginType == "pwd" && this.formData.password.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}


				//登录按钮转圈
				_this.isRotate = true
				let promise
				if (this.loginType == "code") {
					promise = this.$api.api('loginOrRegisterBySms', {
						"phone": _this.formData.phoneData,
						"code": _this.formData.verCode,
						"inviteCode": uni.getStorageSync('inviteCode') ? uni.getStorageSync('inviteCode'):0
					})
				} else if (this.loginType == "pwd") {
					promise = this.$api.api('login', {
						"username": _this.formData.phoneData,
						"password": _this.formData.password
					})
				}

				promise.then(res => {

					if (res.code == 200) {
						this.$store.dispatch('setTokenAndBack', res.data).then(res => {
							this.$store.dispatch('getUserInfo')
						}).catch((e) => {
							resolve(false);
						});

					} else {
						uni.showToast({
							icon: 'none',
							position: 'bottom',
							title: res.msg
						});
					}
					//登录按钮取消转圈
					setTimeout(function() {
						_this.isRotate = false
					}, 1000)
				})
				//登录按钮取消转圈
				setTimeout(function() {
					_this.isRotate = false
				}, 5000)
			},
			login_weixin() {
				if (this.showAgree == false) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '请先同意《协议》'
					});
					return false;
				}
				this.show = true
			}
		}
	}
</script>

<style>
	@import url("../../components/login/css/icon.css");
	@import url("../../components/login/css/main.css");
</style>
